import api from "../../api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRoute, useRouter } from "vue-router";
// 获取字典
export function getDictionariesList(groupName, paramValue) {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("auth/client/dictionary/getListByGroupName", {
        groupName: groupName,
      })
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
export function getDictionaries(groupName) {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("auth/client/dictionary/getListByGroupName", {
        groupName: groupName,
      })
      .then((res) => {
        console.log(res);
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
export function getEnterpriseInfo() {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("auth/client/business/auth/getDetail", {})
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
export function getloginAccountInfo() {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("auth/client/account/loginAccountInfo", {})
      .then((res) => {
        console.log(res);
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
export function getBusinessDetail() {
  return new Promise(function (resolve, reject) {
    api
      .getListAPI("auth/client/business/auth/getDetail", {})
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
export function getAPI(url, Obj) {
  const router = useRouter();
  return new Promise(function (resolve, reject) {
    api
      .getListAPI(url, Obj)
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else if (res.data.code == 401) {
          router.push('./login')
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
export function postAPI(url, Obj) {
  const router = useRouter();
  return new Promise(function (resolve, reject) {
    api
      .getListAPI(url, Obj)
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else if (res.data.code == 401) {
          router.push('./login');
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
//乘法精度计算
export function accMul(arg1, arg2) {
  var m = 0,
    s1 = arg1.toString(),
    s2 = arg2.toString();
  try {
    m += s1.split('.')[1].length;
  } catch (e) { }
  try {
    m += s2.split('.')[1].length;
  } catch (e) { }
  return (Number(s1.replace('.', '')) * Number(s2.replace('.', ''))) / Math.pow(10, m);
}
// //除法精度计算
export function accDiv(arg1, arg2) {
  var r1 = 0, r2 = 0, m, s1 = arg1.toString(), s2 = arg2.toString();
  try {
    if (s1.split(".")[1] != undefined)
      r1 = s1.split(".")[1].length;
  } catch (e) { }
  try {
    if (s2.split(".")[1] != undefined)
      r2 = s2.split(".")[1].length;
  } catch (e) { }
  m = Math.pow(10, Math.max(r1, r2));
  return (accMul(arg1, m)) / (accMul(arg2, m));
}
export function platform() {
  if (api.servePlatform() == 4) {
    return "包头林草"
  } else {
    return "林业"
  }
}
// 文件上传
export function getUploaddata() {
  return {
    url: api.getUrl() + "external/file/upload", //接口名称
    data: {
      upType: "fc",
      sort: Math.floor(Math.random() * 20) + 1,
    },
    headers: {
      token: localStorage.getItem("token"),
      Authorization: "Bearer " + localStorage.getItem("token"),
    },
  }
}
// 判断文件类型
export function isPictureFormat(format) {
  // 判断是否是图片格式
  if (format.indexOf('png') > -1 || format.indexOf('jpg') > -1 || format.indexOf('jpeg') > -1 || format.indexOf('gif') > -1 || format.indexOf('svg') > -1 || format.indexOf('bmp') > -1 || format.indexOf('ai') > -1 || format.indexOf('psd') > -1) {
    return true;
  } else {
    return false;
  }
}
// 获取文件类型
export function getfileType(Str) {
  var pattern = /\.{1}[A-Za-z]{1,}$/;
  var result = pattern.exec(Str);
  return result[0];
}
// 生态账户表单提交
export function EcoAccountFormSubmission(url, form) {
  return new Promise(function (resolve, reject) {
    api
      .postFormAPI(url, form)
      .then((res) => {
        if (res.data.code == 1) {
          ElMessage.success("提交成功！");
          resolve(res.data);
        } else {
          ElMessage.error(res.data.message);
        }
      })
      .catch((err) => { });
  });
}
// 获取 1依据标准 2测算机构 3测算方法
export function getCalculateList(index) {
  const router = useRouter();
  let url = "";
  if (index == 1) {//1依据标准
    url = "ecology/client/ecologyFollowRule/getClientList";
  } else if (index == 2) {//2测算机构
    url = "ecology/client/ecologyCalculateOrganization/getClientList";
  } else if (index == 3) {//3测算方法
    url = "ecology/client/ecologyCalculateMethod/getClientList";
  }
  return new Promise(function (resolve, reject) {
    api
      .getListAPI(url, {})
      .then((res) => {
        if (res.data.code == 1) {
          resolve(res.data.data);
        } else if (res.data.code == 401) {
          router.push('./login');
        } else {
          if (index == 1) {
            ElMessage.error(res.data.message);
          }
        }
      })
      .catch((err) => { });
  });
}